<?php

namespace ticket\modules\frontend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_info".
 *
 * @property int $tid
 * @property int $aid
 * @property int $app_id
 * @property string $name
 * @property int $type_id
 * @property string $is_advance
 * @property string $ticket_sn
 * @property string $ori_price
 * @property string $ruling_price
 * @property string $state
 * @property int $sort
 * @property string $create_time
 * @property string $update_time
 */
class TicketInfo extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_info';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['aid', 'app_id', 'name', 'ticket_sn', 'ori_price', 'ruling_price'], 'required'],
            [['aid', 'app_id', 'type_id', 'sort'], 'integer'],
            [['is_advance', 'state'], 'string'],
            [['ori_price', 'ruling_price','vip_price','now_ticket_price'], 'number'],
            [['create_time', 'update_time'], 'safe'],
            [['name', 'ticket_sn','vip_ticket_sn','now_ticket_sn'], 'string', 'max' => 64],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'tid' => 'Tid',
            'aid' => 'Aid',
            'app_id' => 'App ID',
            'name' => 'Name',
            'type_id' => 'Type ID',
            'is_advance' => 'Is Advance',
            'ticket_sn' => 'Ticket Sn',
            'ori_price' => 'Ori Price',
            'ruling_price' => 'Ruling Price',
            'state' => 'State',
            'sort' => 'Sort',
            'create_time' => 'Create Time',
            'update_time' => 'Update Time',
        ];
    }


    /*** 获取单个门票信息
     * @param $tid
     * @return array|null|\yii\db\ActiveRecord
     */
    public function getInfo($tid)
    {
        $data = self::find()->where('tid = :id',[':id' => $tid])->asArray()->one();
        return $data;
    }

    public function getTicketInfo($tid)
    {
        $ticketInfo = self::find()->where('tk_ticket_info.tid = :tid',[':tid' => $tid])->
        joinWith('additional')->asArray()->one();

        if (!empty($ticketInfo['additional']['image_url'])){
            $ticketInfo['additional']['image_url'] = json_decode($ticketInfo['additional']['image_url'],true)[0]['url'];
        }else{
            $ticketInfo['additional']['image_url'] = '';
        }
        if (!empty($ticketInfo['additional']['albums_url'])){
            $ticketInfo['additional']['albums_url'] = json_decode($ticketInfo['additional']['albums_url'],true);
        }else{
            $ticketInfo['additional']['albums_url'] = [];
        }
        return $ticketInfo;
    }

    public static function ticketInfo($tid)
    {
        $sql = "select a.tid, a.app_id, a.name, a.ticket_sn, a.category, a. ori_price, a.ruling_price,a.now_ticket_price,a.now_ticket_sn,a.vip_ticket_sn,a.vip_price, b.image_url, b.albums_url,b.additional,b.morn_reserve_time, b.after_reserve_time, b.buy_notice, b.reserve_time, b.channel_id from tk_ticket_info a left join tk_ticket_additional b on a.tid = b.tid where a.tid = ".$tid;
        $ticketInfo = Yii::$app->db->createCommand($sql)->queryOne();
        if (!empty($ticketInfo['image_url'])){
            $ticketInfo['image_url'] = json_decode($ticketInfo['image_url'],true)[0]['url'];
        }
        if (!empty($ticketInfo['albums_url'])){
            $ticketInfo['albums_url'] = json_decode($ticketInfo['albums_url'],true);
        }
        if (!empty($ticketInfo['additional'])){
            $ticketInfo['additional'] = json_decode($ticketInfo['additional'],true);
        }
        if(!empty($ticketInfo['morn_reserve_time'])){
            $morn_reserve_time = json_decode($ticketInfo['morn_reserve_time'],true);
            $startTime =  date("H:i",strtotime($morn_reserve_time[0]));
            $endTime =  date("H:i",strtotime($morn_reserve_time[1]));
            $ticketInfo['morn_reserve_time'] = $startTime . ' ~ '. $endTime;
//                $ticketInfo['morn_reserve_time'] = $endTime;
        }
        if(!empty($ticketInfo['after_reserve_time'])){
            $after_reserve_time = json_decode($ticketInfo['after_reserve_time'],true);
            $startTime =  date("H:i",strtotime($after_reserve_time[0]));
            $endTime =  date("H:i",strtotime($after_reserve_time[1]));
            $ticketInfo['after_reserve_time'] = $startTime . ' ~ '. $endTime;
//                $ticketInfo['after_reserve_time'] = $endTime;
        }
        $ticketInfo['todayTime'] = [];
        $ticketInfo['otherTime'] = [];
        if(!empty($ticketInfo['reserve_time'])){
            $reserveTime = explode(',',$ticketInfo['reserve_time']);
            sort($reserveTime);
            $nowH = date("H:i");
            unset($ticketInfo['reserve_time']);
            foreach ($reserveTime as $key => $val){
                $time = substr($val,-5);
                if ($time > $nowH){
                   $ticketInfo['todayTime'][] = $val;
                }

            }
            $ticketInfo['otherTime'] = $reserveTime;
        }

        return $ticketInfo;
    }

    public function getAdditional()
    {
        $data = $this->hasOne(TicketAdditional::className(),['tid' => 'tid']);
        return $data;
    }

    public function getAttrInfo()
    {
        $data = $this->hasMany(TicketAddionalAttr::className(),['tid' => 'tid']);
        return $data;
    }

    public function getTicketDate()
    {
        $data = $this->hasMany(TicketDate::className(),['tid' => 'tid'])->where(['>=','tk_ticket_date.date',date("Y-m-d")])->andWhere(['>','tk_ticket_date.inventory',0]);
        return $data;
    }

    public function getRule()
    {
        $data = $this->hasOne(TicketQuotaRule::className(),['tid' => 'tid']);
        return $data;
    }
}
